package com.itjeffrey.autocode.util;

import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.framework.AopProxy;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * 普通类获取SpringBean的工具类
 *
 * @From: Jeffrey
 * @Date: 2020/11/15
 */
@Component
public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext appContext;

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        Objects.requireNonNull(appContext, "application context require NonNull");
        return appContext.getBean(name);

    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        Objects.requireNonNull(appContext, "application context require NonNull");
        return appContext.getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        Objects.requireNonNull(appContext, "application context require NonNull");
        return appContext.getBean(name, clazz);
    }

    /**
     * 基于applicationContext的发布订阅
     *
     * @param event
     */
    public static void publish(ApplicationEvent event) {
        if (Objects.isNull(appContext)) {
            return;
        }
        appContext.publishEvent(event);
    }

    /**
     * 获取目标对象
     * @param proxy 代理对象
     * @return
     */
    public static Object getTarget(Object proxy) throws Exception{
        //not proxy object, return
        if(!AopUtils.isAopProxy(proxy)){
            return proxy;
        }
        //jdk dynamic proxy
        if(AopUtils.isJdkDynamicProxy(proxy)){
            return getJdkDynamicProxyTarget(proxy);
        }else{
        //cglib dynamic proxy
            return getCglibDynamicProxyTarget(proxy);
        }
    }

    /**
     * get cglib dynamic proxy
     * @param proxy
     * @return
     * @throws Exception
     */
    private static Object getCglibDynamicProxyTarget(Object proxy) throws Exception{
        Field cglib$CALLBACK_0 = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0");
        cglib$CALLBACK_0.setAccessible(true);
        Object dynamicAdvisedInterceptor = cglib$CALLBACK_0.get(proxy);
        Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised");
        advised.setAccessible(true);
        AdvisedSupport advisedSupport = (AdvisedSupport) advised.get(dynamicAdvisedInterceptor);
        return advisedSupport.getTargetSource().getTarget();
    }

    private static Object getJdkDynamicProxyTarget(Object proxy) throws Exception{
        Field h = proxy.getClass().getSuperclass().getDeclaredField("h");
        h.setAccessible(true);
        AopProxy aopProxy = (AopProxy) h.get(proxy);
        Field advised = aopProxy.getClass().getDeclaredField("advised");
        advised.setAccessible(true);
        AdvisedSupport advisedSupport = (AdvisedSupport) advised.get(aopProxy);
        return advisedSupport.getTargetSource().getTarget();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (Objects.isNull(appContext)) {
            appContext = applicationContext;
        }
    }
}